package com.kunchi.platform.entity.base;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@MappedSuperclass
public abstract class AbstractEntity<ID> implements Entity<ID> {
    @Column(name = "create_time", nullable = false, updatable = false)
    private Date createTime;

    @Column(name = "update_time", nullable = false)
    private Date updateTime;

    protected void touchCreateTime() {
        this.createTime = new Date();
    }

    protected void touchUpdateTime() {
        this.updateTime = new Date();
    }

    @PrePersist
    protected void onCreate() {
        touchCreateTime();
        touchUpdateTime();
    }

    @PreUpdate
    protected void onUpdate() {
        touchUpdateTime();
    }
}
